package top.hidoctor.auscultation.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PathEffect;
import android.support.v4.view.MotionEventCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import top.hidoctor.auscultation.bean.RectBean;
import top.hidoctor.auscultation.widget.listener.OnRectClickListener;

public class MyRectView extends View {


    private String color;
    private String id;
    private float x = 50;
    private float y = 50;
    private float w = 210;
    private float h = 150;
    private float downX;
    private float downY;


    public OnRectClickListener onRectClickListener;

    public void setOnRectClickListener(OnRectClickListener onRectClickListener) {
        this.onRectClickListener = onRectClickListener;
    }

    public MyRectView(Context context) {
        super(context);
    }

    public MyRectView(final Context context, RectBean rectBean) {
        super(context);
        this.x = rectBean.getX();
        this.y = rectBean.getY();
        this.w = rectBean.getW();
        this.h = rectBean.getH();
        this.id = rectBean.getId();
        this.color = rectBean.getColor();

        this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = MotionEventCompat.getActionMasked(event);
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        downX = event.getX();
                        downY = event.getY();
                        if (downX < w && downX > x && downY > y && downY < h) {
                            Log.d("img", "点击框");
                            if (onRectClickListener != null) {
                                onRectClickListener.onClick(v, id);
                            }
                            return true;
                        }
                        return false;
                }
                return true;
            }
        });

    }

    public MyRectView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint(); // 笔触
        paint.setAntiAlias(true); // 反锯齿
        paint.setColor(TextUtils.isEmpty(color) ? Color.YELLOW : Color.parseColor(color));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        PathEffect effects = new DashPathEffect(new float[]{10, 10, 10, 10}, 2);
        paint.setPathEffect(effects);
        canvas.drawRect(x, y, w, h, paint);
        super.onDraw(canvas);
    }


}
